package com.tau.egonetwork.gui.linkedin;

import java.util.concurrent.Future;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.tau.egonetwork.common.Configurations;
import com.tau.egonetwork.gui.Environment;
import com.tau.egonetwork.gui.core.NetworkTab;
import com.tau.egonetwork.gui.core.PopupGUI;
import com.tau.egonetwork.gui.interfaces.IPopupAnswerToTab;
import com.tau.egonetwork.gui.interfaces.IRequestGUI;
import com.tau.egonetwork.linkedin.LinkedinAuthRequest;
import com.tau.egonetwork.profiles.MainProfile;
import com.tau.egonetwork.profiles.ProfileFile;
import com.tau.egonetwork.profiles.ProfileFile.SocialNetworks;
import com.tau.egonetwork.threads.core.EgoNetworkCallable;
import com.tau.egonetwork.threads.core.ThreadPool;

public class LinkedinTab extends NetworkTab {

    public LinkedinTab(IRequestGUI linkedinGUIRequest) { 
    	super(linkedinGUIRequest);
    }
	
	@Override
	protected void openPopup(Display display, Shell shell, IPopupAnswerToTab popupAnswer) {
		PopupGUI.displayBrowser(display,
				LinkedinAuthRequest.generateAuthRequest().generateRequestURL(),
				"New LinkedIn Profile",
				new LinkedinPopupResponse(shell, popupAnswer, _networkRequestGUI),
				Configurations.linkedinPopupWidth(),
				Configurations.linkedinPopupHeight());

	}

	
	@Override
	protected String getTabText() {
		return ("  Linkedin");
	}

	@Override
	protected Image getTabImage() {
		return Environment.ICON_LINKEDIN;
	}

	@Override
	protected Image getBackground() {
		return Environment.LINKEDIN_BG;
	}

	@Override
	protected Image getLogo() {
		return Environment.LINKEDIN_LOGO;
	}

	@Override
	protected Image getNewButtonImage() {
		return Environment.LINKEDIN_PROFILE;
	}

	@Override
	protected Image getOpenButtonImage() {
		return Environment.LINKEDIN_OPEN;
	}

	@Override
	protected void onOpenButtonClicked(Shell parent) {
		LinkedinFileOpenDialog ffod = new LinkedinFileOpenDialog(parent);
		final String filename = (String)ffod.open();
		if (filename != null) {
			_networkRequestGUI.onRequestIssued();
			_networkRequestGUI.onResponseReceived(ProfileFile.getProfileNameFromFileName(filename, SocialNetworks.Linkedin));

			ThreadPool.submitTask(new EgoNetworkCallable<Future<Void>, Void>() {
				
				@Override
				public Void call() throws Exception {
					MainProfile<?> profile = ProfileFile.openFile(filename);
					if (profile == null) {
						_networkRequestGUI.onResponseFailed(true, "Error while opening file.");
					} else {
						_networkRequestGUI.onResponseFinishedSuccessfuly(true, profile);
					}
					return null;
				}
			});
			
		}
	}


}
